package com.myrecycleview631wrydata.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import cst.a613ld.recycleview613ld.R;
import cst.a613ld.recycleview613ld.bean.UserBean;
import cst.a613ld.recycleview613ld.iface.LoginListener;
import cst.a613ld.recycleview613ld.model.LoginModel;

/**
 * Created by 64410 on 2017/5/25.
 */

public class LoginActivity  extends AppCompatActivity implements View.OnClickListener,LoginListener{
    private EditText ed_username;
    private EditText ed_password;
    private Button bu_login;
    private Button bu_regtister;
    private Context context;
    private String userID;
    private String password;
    private CheckBox checkBox_remember;
    private SharedPreferences preferences;
    private SharedPreferences preference;
    final static String PREF_NAME="loginPref";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginactivity1);
        init();
        context =LoginActivity.this;
        preferences= getSharedPreferences(PREF_NAME,MODE_PRIVATE);
        boolean isChecked=preferences.getBoolean("isChecked",false);
        if(isChecked)
        {
            String userName = preferences.getString("userName", "");
            String password = preferences.getString("password", "");
            ed_username.setText(userName);
            ed_password.setText(password);
            checkBox_remember.setChecked(isChecked);
        }
    }
    private void init()
    {
        ed_username=(EditText)findViewById(R.id.editText_uesrname);
        ed_password=(EditText)findViewById(R.id.editText_password);
        bu_login=(Button)findViewById(R.id.button_login);
        bu_regtister=(Button)findViewById(R.id.button_regtister);
        bu_login.setOnClickListener(this);bu_regtister.setOnClickListener(this);
        checkBox_remember=(CheckBox)findViewById(R.id.checkBox_remember);

    }
    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.button_login:
                userID=ed_username.getText().toString();
                password=ed_password.getText().toString();
                LoginModel mode=new LoginModel();
                mode.login(userID,password,this);
                break;
            case R.id.button_regtister:
            {
                Intent intent_reg = new Intent(LoginActivity.this, RegtisterActivity.class);
                startActivity(intent_reg);
                break;
            }
            default:break;
        }
    }

    @Override
    public void onResponse(UserBean userBean) {
        Intent intent =new Intent(context,MainActivity.class);
        Toast.makeText(context,"Ok",Toast.LENGTH_SHORT).show();
        String sessionId=userBean.getSessionid();
        SharedPreferences.Editor editor =preferences.edit();

        editor.putString("sessionId",sessionId);
        editor.putBoolean("isChecked",checkBox_remember.isChecked());
        if(checkBox_remember.isChecked()){
            editor.putString("userName",ed_username.getText().toString());
            editor.putString("password",ed_password.getText().toString());

        }
        editor.commit();
        preference= getSharedPreferences(PREF_NAME,MODE_PRIVATE);
        String s=preference.getString()

        startActivity(intent);
        finish();

    }

    @Override
    public void onFail(String msg) {

    }
}
